/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.server;

import cn.ac.iie.di.global.GlobalParas;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author shipengfeng
 */
public class Monitor implements Runnable {

        public static Logger log = Logger.getLogger(Monitor.class.getName());

        @Override
        public void run() {
                while (true) {
                        try {
                                log.info("GlobalParas.oriMsgPool size is " + GlobalParas.oriMsgPool.size());
                                log.info("GlobalParas.dwdMsgPool size is " + GlobalParas.dwdMsgPool.size());
                                log.info("GlobalParas.urlAndAddrPool size is " + GlobalParas.urlAndAddrPool.size());
                                log.info("GlobalParas.sendMsgPool size is " + GlobalParas.sendMsgPool.size());
                                log.info("GlobalParas.sendMsgPool2 size is " + GlobalParas.sendMsgPool2.size());
                                log.info("GlobalParas.myRequestPool size is " + GlobalParas.myRequestPool.size());
                                log.info("GlobalParas.send2MyHttpRequestPool size is " + GlobalParas.send2MyHttpRequestPool.size());
                                Thread.sleep(5000);

//                                Date current = new Date();
//                                SimpleDateFormat sft = new SimpleDateFormat("yyyyMMdd");// 日期格式化
                                Calendar c = Calendar.getInstance();

                                String currentDate = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DATE);
                                log.info("current date : " + currentDate + " dateinfo : " + GlobalParas.dateInfo + " countQuest num : " + GlobalParas.countQuest.get());
                                if (!currentDate.equals(GlobalParas.dateInfo)) {
                                        GlobalParas.dateInfo = currentDate;
                                        GlobalParas.countQuest = new AtomicInteger(0);
                                        log.info("set countQuest to 0 ");
                                        log.info("GlobalParas.dateInfo is : " + currentDate);
                                }

                                if (GlobalParas.killMonitor) {
                                        log.info("monitor is exit ");
                                        break;
                                }
                        } catch (Exception e) {
                        }
                }

        }

        private static boolean isSameDate(Date date1, Date date2) {
                Calendar cal1 = Calendar.getInstance();
                cal1.setTime(date1);

                Calendar cal2 = Calendar.getInstance();
                cal2.setTime(date2);

                boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                                .get(Calendar.YEAR);
                boolean isSameMonth = isSameYear
                                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
                boolean isSameDate = isSameMonth
                                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                                .get(Calendar.DAY_OF_MONTH);

                return isSameDate;
        }

}
